//
//  WebAuthorizeViewController.m
//  iFlickr
//
//  Created by 张 霄鹤 on 09-5-31.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "WebAuthorizeViewController.h"

#import "DataManager.h"


@implementation WebAuthorizeViewController

@synthesize web;
@synthesize active;
@synthesize bFinish;
@synthesize url;

/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	self.navigationItem.title = @"flickr";
	if(self.url){
		NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:self.url];
		[req setTimeoutInterval:120];
		[self.web loadRequest:req];
	}
}

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
	self.web = nil;
	self.active = nil;
	self.bFinish = nil;
	self.url = nil;
}

#pragma mark button_done_auth_finish

-(IBAction)continueGetToken:(id)sender{
	DataManager *dataManager = [DataManager sharedDataManager];
	/*NSString *token = */
	[dataManager getUserToken];
	[self.navigationController popToRootViewControllerAnimated:YES];
	
	[[NSNotificationCenter defaultCenter] postNotificationName:@"RVC_Reload" object:nil];
}


#pragma mark UIWebViewDelegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request
	navigationType:(UIWebViewNavigationType)navigationType {
	return YES;
}

- (void)webViewDidStartLoad:(UIWebView *)webView{
	UIBarButtonItem *bitem = [[UIBarButtonItem alloc] initWithCustomView:self.active];
	[self.navigationItem setRightBarButtonItem:bitem];
	[self.active startAnimating];
	[bitem release];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView{
	[self.active stopAnimating];
	[self.active removeFromSuperview];
	[self.navigationItem setRightBarButtonItem:bFinish];
}

/**
 * UIWebView 联网请求出错时触发
 */
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
	[self.active stopAnimating];
	[self.active removeFromSuperview];
	UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:[error localizedFailureReason] delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
	[alert show];
	[alert release];
}



#pragma mark -

- (void)dealloc {
    [super dealloc];
}


@end
